from django.shortcuts import render
from django.views import View

from .utils import get_categories
from contents.models import Content,ContentCategory


class Index(View):
    """首页视图"""
    def get(self,request):
        # 准备一个大字典{'广告类别':'该类别所有广告',.....}
        contents = {}
        # 查出所有的广告类别(查询集）
        content_qs = ContentCategory.objects.all()
        for content_cat in content_qs:
            contents[content_cat.key] = content_cat.content_set.filter(status=True).order_by('sequence')

        context = {
            'categories': get_categories(),
            'contents': contents
        }
        return render(request, 'index.html', context)


